<template>
	<view class="username">
		<form class="center" @submit="submitname">
			<input type="text" :value="username" name="username" />
			<button size="mini" form-type="submit">提交修改</button>
		</form>
	</view>
</template>

<script>
	import {request} from '../../request/index.js'
	export default{
		data(){
			return {
				username:'',
			}
		},
		onLoad(e){
			console.log(e.username)
			this.username=e.username
		},
		methods:{
			submitname(e){
				this.username=e.detail.value.username
				let userid=uni.getStorageSync("userinfo").id
				 uni.request({
				    url: 'http://127.0.0.1:3000/user/username',
				    data: {
				        username: e.detail.value.username,
				        id: userid
				    },
					method:"POST",
				    success: function(res) {
				        console.log(res.data.status);
						if(res.data.status==200){
							let data=uni.getStorageSync("userinfo")
							data.username=e.detail.value.username
							uni.setStorageSync("userinfo",data)
							uni.showToast({
								title:"修改成功",
								duration:1500
							})
							uni.redirectTo({
								url:"/pages/me/meinfo"
							})
						}
				    }
				});
				
			}
		}
	}
</script>

<style lang="less" scoped>
	.username{
		width: 100vw;
		height: 100vh;
		position: relative;
		form{
			width: 60%;
			height: 20%;
			text-align: center;
			border: 1px solid #ccc;
			padding: 20upx;
			input{
				width: 100%;
				height: 100upx;
				line-height: 100upx;
				border-bottom: 1px solid #ccc;
				margin-bottom: 50upx;
			}
			button{
				
			}
		}
	}
</style>
